<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="80px"
      class="frominfo"
    >
      <!-- 搜索栏部分 -->
      <div class="from-item">
        <el-form-item label="参数名" prop="variableName">
          <el-input
            v-model="queryParams.variableName"
            placeholder="请输入参数名"
            clearable
          />
        </el-form-item>
        <el-form-item label="变量类型">
          <el-select
            v-model="queryParams.variableType"
            placeholder="请下拉选择"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in itemList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </div>
      <div class="from-Btn">
        <el-form-item>
          <el-button
            class="blue-button"
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >查询</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </div>
    </el-form>

    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="add"
          style="margin-bottom: 10px"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table :data="tableData" v-loading="loading" style="width: 100%">
      <el-table-column label="变量名称" prop="variableName"></el-table-column>
      <el-table-column label="变量类型">
        <template slot-scope="scope">
          {{ handleType(scope.row.variableType) }}
        </template>
      </el-table-column>
      <el-table-column
        label="变量描述"
        prop="variableDescribe"
      ></el-table-column>
      <!-- <el-table-column label="创建时间" prop="createTime"></el-table-column> -->
      <el-table-column label="更新时间" prop="updateTime"></el-table-column>
      <el-table-column label="操作" prop="defaultValue">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wxassistant:wcbushallinfo:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wxassistant:wcbushallinfo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="变量名称" prop="variableName">
          <el-input v-model="form.variableName" placeholder="请输入变量名称" />
        </el-form-item>
        <el-form-item label="描述" prop="variableDescribe">
          <el-input
            v-model="form.variableDescribe"
            type="textarea"
            placeholder="请简单描述"
          />
        </el-form-item>
        <el-form-item label="变量类型">
          <el-select
            v-model="form.variableType"
            placeholder="请下拉选择"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in itemList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认值" prop="defaultValue">
          <el-input
            v-model="form.defaultValue"
            placeholder="请输入默认值"
            :disabled="
              form.variableType == 1 || form.variableType == 2 ? false : true
            "
          ></el-input>
        </el-form-item>

        <el-form-item
          label="上传"
          prop="variableDescribe"
          required
          v-if="form.variableType == 3 || form.variableType == 4"
        >
          <el-upload
            ref="variableDescribeRef"
            :file-list="variableDescribeList"
            :action="variableDescribeAction"
            :before-upload="variableDescribeBeforeUpload"
            :on-success="handleFileSuccess"
            :headers="headers"
          >
            <el-button size="small" type="primary" icon="el-icon-upload"
              >点击上传</el-button
            >
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="confirm"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  adminReadParams,
  adminAddParams,
  adminDelParams,
  adminEditParams,
} from "@/api/system/params";
import { getToken } from "@/utils/auth";

export default {
  name: "index",
  data() {
    return {
      title: "新增变量",
      loading: true,
      open: false,
      html: "",
      form: {
        variableName: "",
        variableType: "",
        variableDescribe: "",
        defaultValue: "",
      },
      tableData: [],
      variableDescribeList: [],
      headers: { Authorization: "Bearer " + getToken() },
      variableDescribeAction: process.env.VUE_APP_BASE_API + "/common/upload",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        variableId: "",
        variableName: "",
        variableType: "",
        variableDescribe: "",
      },
      total: 0,
      rules: {
        variableName: [
          { required: true, message: "变量名称不能为空", trigger: "blur" },
        ],
        variableType: [
          { required: true, message: "变量类型不能为空", trigger: "blur" },
        ],
      },
      itemList: [
        { label: "其他", value: 0 },
        { label: "文字", value: 1 },
        { label: "链接", value: 2 },
        { label: "图片", value: 3 },
        { label: "文件", value: 4 },
      ],
    };
  },
  methods: {
    add() {
      this.open = true;
      this.form.variableName = "";
      this.form.variableType = "";
      this.form.variableDescribe = "";
      this.form.defaultValue = "";
    },
    cancel() {
      this.open = false;
    },
    confirm() {
      this.open = false;
      if (this.form.variableId) {
        adminEditParams(this.form).then((res) => {
          if (res.code == 200) {
            this.getList();
            this.$message.success("修改成功");
          }
        });
      } else {
        adminAddParams(this.form).then((res) => {
          if (res.code == 200) {
            this.$message.success("新增成功");
            this.getList();
          }
        });
      }
      // console.log(this.html);
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.handleQuery();
      this.queryParams.variableName = "";
      this.queryParams.variableType = "";
      this.getList();
    },
    variableDescribeBeforeUpload(file) {
      let isRightSize = file.size / 1024 / 1024 < 2;
      if (!isRightSize) {
        this.$message.error("文件大小超过 2MB");
      }
      return isRightSize;
    },
    handleFileSuccess(response, file, fileList) {
      this.form.defaultValue = response.url;
    },
    getList() {
      this.loading = true;
      adminReadParams(this.queryParams).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    handleUpdate(row) {
      this.open = true;
      this.form = { ...row };
      this.form.variableId = row.variableId;
    },
    handleDelete(row) {
      this.$confirm(
        "是否确认删除变量名称为" + row.variableName + "的数据项?",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        adminDelParams(row.variableId).then((res) => {
          if (res.code == 200) {
            this.getList();
            this.$message.success("删除成功");
          }
        });
      });
    },
    handleType(num) {
      switch (num) {
        case 0:
          return "其他";
        case 1:
          return "文字";
        case 2:
          return "链接";
        case 3:
          return "图片";
        case 4:
          return "文件";
      }
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

<style lang="scss">
.app-container {
  background-color: #f6f9fb;
}
.frominfo {
  display: flex;
  background-color: #fff;
  padding: 10px;
  padding-bottom: 0;
  margin-bottom: 20px;

  .from-item {
    display: flex;
    flex-wrap: wrap;
    width: 85%;
  }

  .from-Btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 20%;
  }
}
.add-upload {
  display: flex;
  width: 400px;
  border-radius: 5px;
  border: 1px solid #dcdfe6;
  justify-content: center;
  align-items: center;
  padding: 20px 0;
  margin-left: 15px;
  margin-bottom: 20px;
}
</style>
